package com.saodisheng.controller;

import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;
import com.saodisheng.util.TimeUtil;
import com.saodisheng.service.AdminDbService;
import com.saodisheng.constant.AttributeConstant;
import org.springframework.beans.factory.annotation.Autowired;

public class AdminDbController extends BaseController {
	@Autowired
	private AdminDbService dbService;
	
	public void backupRestore() {
		render("backup_restore.html");
	}
	
	public void list() {
		render("db_file_list.html");
	}
	
	public void backup() {
		if (getPara("backupPath") == null || getPara("backupPath").equals("")) {
			renderJson(Ret.fail("msg", "请填写备份地址"));
			return;
		}
		if (dbService.exportDatabaseTool(AttributeConstant.WEBIP, AttributeConstant.mysqlUser, AttributeConstant.mysqlPassword,
				  getPara("backupPath"), TimeUtil.getStringDate() + ".sql", "fcms")) {
			  System.out.println("数据库成功备份！！！");
			  renderJson(Ret.ok("msg", "数据库成功备份"));
		 }
		else {
			  System.out.println("数据库备份失败！！！");
			  renderJson(Ret.fail("msg", "数据库备份失败"));
		 }
	}
	
	public void restore() {
		UploadFile file = getFile("dbFile", "db");
		
		String path = file.getUploadPath() + "\\"+file.getFileName();
		System.out.println(path);
		try {
			Ret ret = dbService.restore("aa", "fcms");
			renderJson(ret);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
